/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
package cgl.narada.performance;

import java.util.Enumeration;
import java.util.Hashtable;

import cgl.narada.transport.Link;
import cgl.narada.transport.MonitoredLink;
import cgl.narada.transport.TransportException;
import cgl.narada.transport.TransportHandler;
import cgl.narada.transport.TransportHandlerImpl;
import cgl.narada.transport.udp.UDPLink;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Company: </p>
 * @author Gurhan Gunduz
 * @version 1.0
 */

public class MonitoringServiceImpl implements MonitoringService {

  private static TransportHandler t_handler;
  private MeasurementInitiator m_initiator;
  private String usergroup="A";

  public MonitoringServiceImpl(TransportHandlerImpl th) {
    t_handler=th;
  //    m_initiator=new MeasurementInitiator();

  }

  public MonitoringServiceImpl() {
    m_initiator=new MeasurementInitiator();

  }

  public void setTransportHandler(TransportHandlerImpl th) 
    throws TransportException{
    t_handler=th;
  }

  public void setMeasurementInitiatior(MeasurementInitiator mi) 
    throws MonitoringServiceException{
    m_initiator=mi;
  }

  public MeasurementInitiator getMeasurementInitiator(){
    return m_initiator;
  }

  public void startMeasurementInitiator(){
    //set transporthandler of
    m_initiator.setTransportHandler(t_handler);
    m_initiator.start();
  }
  public void setMeasurementInterval(String linkID, long interval){

  }

  public long getMinimumMeasurementInterval(){
    return 0;
  }

  public long getMeasurementInterval(){
    return 0;
  }

  public void showAllLinks(){
    Hashtable links=new Hashtable();
    links=t_handler.getRegisteredLinks();
    Enumeration e = links.elements();

    while (e.hasMoreElements()) {
      Link _link = (Link)e.nextElement();
      String key=_link.getLinkId();
      System.out.println("Link_id...:"+key);
    }
  }

//show
  public void showMonitoredLinks(){
   Hashtable links=new Hashtable();
   links=m_initiator.getLinks();
   Enumeration e = links.elements();

   while (e.hasMoreElements()) {
     Link _link = (Link)e.nextElement();
     String key=_link.getLinkId();
     System.out.println("Link_id...:"+key);
   }
  }

//cycles through all links and find the measuring ones.
  public void findMeasuringLinks(){
    Hashtable links=new Hashtable();
    links=t_handler.getRegisteredLinks();
    Enumeration e = links.elements();
    while (e.hasMoreElements()){
      Link link = (Link) e.nextElement();
      if (link.performanceGatheringEnabled()){
        System.out.println("Link is added in Monitoring Service");
        m_initiator.addToListOfManagedLinks(link);
      }
    }
  }

//disable the measurement of all the links.

  public void disableLinkMeasurement(){
    Hashtable links=new Hashtable();
    links=m_initiator.getLinks();
    Enumeration e = links.elements();

    while (e.hasMoreElements()) {
      Link _link = (Link)e.nextElement();
      m_initiator.removeFromListOfManagedLinks(_link);
      _link.setPerformanceGathering(false);
    }
  }


//disable the measurement of a specific link
  public void disableLinkMeasurement(String linkID){
    Hashtable managedLinks=new Hashtable();
    managedLinks=m_initiator.getLinks();

    if (linkID == null) {
      System.out.println("LinkID associated with link is null"+linkID);
      return;
    }
    if (!managedLinks.containsKey(linkID)) {
      System.out.println("Cannot find the link..:"+linkID);
      return;
    }
    //find the link with given linkID and remove it from the managed links.
    Enumeration e = managedLinks.elements();
    while (e.hasMoreElements()) {
//      Link _link = (Link)e.nextElement();
      UDPLink _link = (UDPLink)e.nextElement();

      if (_link.getLinkId().equalsIgnoreCase(linkID)){
        m_initiator.removeFromListOfManagedLinks(_link);
       _link.setPerformanceGathering(false);
      }
    }
  }

//disable the measurement of the specific factor on the link. It gets the list
//of links from the measurement initiator.

  public void  disableLinkMeasurement(String linkID, String factor){
    Hashtable managedLinks=new Hashtable();
    managedLinks=m_initiator.getLinks();
    if (linkID == null) {
      System.out.println("LinkID associated with link is null"+linkID);
      return;
    }
    if (!managedLinks.containsKey(linkID)) {
      System.out.println("Cannot find the link..:"+linkID);
      return;
    }
    //find the link with given linkID and remove it from the managed links.
    Enumeration e = managedLinks.elements();
    while (e.hasMoreElements()) {
      Link _link = (Link)e.nextElement();
      if (_link.getLinkId().equalsIgnoreCase(linkID)){

	if (_link instanceof MonitoredLink) {
	  MonitoredLink _mLink = (MonitoredLink) _link;
	  _mLink.disablePerformanceFactor(factor);//disable the factor
	}
	return;
      }
    }

  }

//enable the measurement of all the links. It gets the list
//of links from the transport handler.

  public void enableLinkMeasurement(){
    Hashtable links=new Hashtable();
    links=t_handler.getRegisteredLinks();
    Enumeration e = links.elements();
    while (e.hasMoreElements()) {
      Link _link = (Link)e.nextElement();
      m_initiator.addToListOfManagedLinks(_link);
      _link.setPerformanceGathering(true);

    }

  }

//enables the specific factor on the specific link.
//it finds the link from the list of links which is hold by transport handler
  public void enableLinkMeasurement(String linkID){
    Hashtable links=new Hashtable();
    links=t_handler.getRegisteredLinks();

    if (linkID == null) {
      System.out.println("LinkID associated with link is null"+linkID);
      return;
    }
    if (!links.containsKey(linkID)) {
      System.out.println("Cannot find the link..:"+linkID);
      return;
    }
    //find the link with given linkID and remove it from the managed links.
    Enumeration e = links.elements();
    while (e.hasMoreElements()) {
//      Link _link = (Link)e.nextElement();
      UDPLink _link = (UDPLink)e.nextElement();
      if (_link.getLinkId().equalsIgnoreCase(linkID)){
         m_initiator.addToListOfManagedLinks(_link);
        _link.setPerformanceGathering(true);
      }
    }
  }//end method


//enables the specific factor on the specific link.
//it finds the link from the list of links which is hold by transport handler
//then calls the enablePerformanceFActor method of the link
  public void enableLinkMeasurement(String linkID, String factor){

    Hashtable links=new Hashtable();
    links=t_handler.getRegisteredLinks();

    if (linkID == null) {
      System.out.println("LinkID associated with link is null"+linkID);
      return;
    }
    if (!links.containsKey(linkID)) {
      System.out.println("Cannot find the link..:"+linkID);
      return;
    }
    //find the link with given linkID and remove it from the managed links.
    Enumeration e = links.elements();
    while (e.hasMoreElements()) {
      Link _link = (Link)e.nextElement();
      if (_link.getLinkId().equalsIgnoreCase(linkID)){
	
	if (_link instanceof MonitoredLink) {
	  MonitoredLink _mLink = (MonitoredLink) _link;
	  _mLink.enablePerformanceFactor(factor);//disable the factor
	}
	
	return;
      }
    }
  }//end method

//sets the user group of the node. This is necessary when grouping the
//the nodes for user access. For example, some nodes will be accesses by
//administrators only whereas others can be accessed by all users.
  public void setUserGroup(String u_group){
    usergroup=u_group;
  }

//returns the user group.
  public String getUserGroup(){
    return usergroup;
  }
}//end class
